package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.model.TbySummarizeData;
import com.zl.tby.service.ITbyAnimalPlantService;
import com.zl.tby.service.ITbySummarizeDataService;
import com.zl.tby.vo.TbySummarizeDataBean;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

/**
 * 概括数据管理控制器
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/summarize_data")
public class TbySummarizeDataController {

    @Inject
    private ITbySummarizeDataService tbySummarizeDataService;
    @Inject
    private ITbyAnimalPlantService tbyAnimalPlantService;

    /**
     * 保存或新增面积
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/area", method = MethodType.POST)
    public R<TbySummarizeData> formArea(@Validated TbySummarizeData entity) {
        try {
            TbySummarizeData db = this.tbySummarizeDataService.getDetail();
            if (db.getId() == null) {
                return tbySummarizeDataService.add(entity);
            }
            db.updateArea(entity);
            return tbySummarizeDataService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 保存或新增森林覆盖率
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/forest", method = MethodType.POST)
    public R<TbySummarizeData> formForest(@Validated TbySummarizeData entity) {
        try {
            TbySummarizeData db = this.tbySummarizeDataService.getDetail();
            if (db.getId() == null) {
                return tbySummarizeDataService.add(entity);
            }
            db.updateForest(entity);
            return tbySummarizeDataService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 保存或新增主要保护物种
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/species", method = MethodType.POST)
    public R<TbySummarizeData> formSpecies(@Validated TbySummarizeData entity) {
        try {
            TbySummarizeData db = this.tbySummarizeDataService.getDetail();
            if (db.getId() == null) {
                return tbySummarizeDataService.add(entity);
            }
            db.updateSpecies(entity);
            return tbySummarizeDataService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 保存或新增植物
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/plant", method = MethodType.POST)
    public R<TbySummarizeData> plant(@Validated TbySummarizeData entity) {
        try {
            TbySummarizeData db = this.tbySummarizeDataService.getDetail();
            if (db.getId() == null) {
                return tbySummarizeDataService.add(entity);
            }
            db.updatePlant(entity);
            return tbySummarizeDataService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 保存或新增动物
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/animal", method = MethodType.POST)
    public R<TbySummarizeData> animal(@Validated TbySummarizeData entity) {
        try {
            TbySummarizeData db = this.tbySummarizeDataService.getDetail();
            if (db.getId() == null) {
                return tbySummarizeDataService.add(entity);
            }
            db.updateAnimal(entity);
            return tbySummarizeDataService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 保存或新增系统服务
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/system_service", method = MethodType.POST)
    public R<TbySummarizeData> system_service(@Validated TbySummarizeData entity) {
        try {
            TbySummarizeData db = this.tbySummarizeDataService.getDetail();
            if (db.getId() == null) {
                return tbySummarizeDataService.add(entity);
            }
            db.updateSystemService(entity);
            return tbySummarizeDataService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 获取详情
     * @return 结果
     */
    @Mapping(value = "/detail", method = MethodType.GET)
    public R<TbySummarizeData> detail() {
        TbySummarizeData entity = this.tbySummarizeDataService.getDetail();
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 获取详情带物种
     * @return 结果
     */
    @Mapping(value = "/detail_full", method = MethodType.GET)
    public R<TbySummarizeDataBean> getDetailFull() {
        TbySummarizeDataBean entity = this.tbySummarizeDataService.getDetailFull();
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

}
